/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package epaper;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;
import java.util.Observable;
import java.util.concurrent.Callable;
import java.util.logging.Level;
import java.util.logging.Logger;


/**
 *
 * @author Puneet
 */
public class Worker implements Callable {

    private final String localStr;
    private final String paper;
    private final int id;

    private Map<String,String> cookies = null;

    public Worker(String localstr, int id) {
        this.localStr = localstr;
        this.id = id;
        this.paper = null;
    }

    public Worker(String localstr, int id, String paper) {
        this.localStr = localstr;
        this.id = id;
        this.paper = paper;
    }
    
    public Worker(String localstr, int id, String paper, Map<String,String> cookies) {
        this.localStr = localstr;
        this.id = id;
        this.paper = paper;
        this.cookies = cookies;
    }
    public ByteArrayInputStream call() throws IOException {
        InputStream raw = null;
        int contentLength = 0;
        URL url = null;
        HttpURLConnection uc = null;
        try {
            
            url = new URL(localStr);
            //HttpURLConnection uc = (HttpURLConnection) url.openConnection(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("localhost", 8080)));
            uc = (HttpURLConnection) url.openConnection();
            //uc.setDoInput(true);
            //uc.setDoOutput(false);
            uc.setRequestMethod("GET");
            if(paper != null && paper.equalsIgnoreCase("MINT")){
                if(cookies != null){
                    
                    String myCookie = "ASP.NET_SessionId="+cookies.get("ASP.NET_SessionId")+"; IsUserReg=True";
                    uc.setRequestProperty("Cookie", myCookie);
                    
                }
                uc.setRequestProperty("Referer", "http://epaper.livemint.com/Default.aspx?BMode=100");
                uc.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.12) Gecko/20101026 AskTbTRL2/3.11.3.15590 Firefox/3.6.12");
            }
            contentLength = uc.getContentLength();
            if(contentLength<1024){
                throw new IOException();
            }
            System.out.println("url :"+localStr);
            raw = uc.getInputStream();
            InputStream in = new BufferedInputStream(raw);
            HT.buf[id] = new byte[contentLength];
            int bytesRead = 0;
            int offset = 0;
            while (offset < contentLength) {
                bytesRead = in.read(HT.buf[id], offset, HT.buf[id].length - offset);
                

                if (bytesRead == -1) {
                    break;
                }
                offset += bytesRead;
            }
            
            //System.out.println("Called notify with "+incrProgress);
            WorkProgress.getInstance().increment();
            
            System.out.println("ContentLength :"+contentLength+ "  Offset : "+offset);
            in.close();
            if (offset != contentLength) {
                try {
                    throw new IOException("Only read " + offset + " bytes; Expected " + contentLength + " bytes");
                } catch (IOException ex) {
                    Logger.getLogger(HT.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

        } catch (IOException ex) {
            //Logger.getLogger(Worker.class.getName()).log(Level.SEVERE, null, ex);

            if (id == 0) {
                throw ex;
            }
            WorkProgress.getInstance().increment();
        } finally {
            try {
                raw.close();
            } catch (IOException ex) {
                //Logger.getLogger(Worker.class.getName()).log(Level.SEVERE, null, ex);
            }
            if(contentLength>1024)
                return new ByteArrayInputStream(HT.buf[id]);
            else
                return null;
        }
    }

    
}
